package com.global.college.ui.fragment.lesson;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.global.college.MainActivity;
import com.global.college.R;
import com.global.college.adapter.CourseAdapter;
import com.global.college.adapter.FilterItemAdapter;
import com.global.college.api.ProgressSubscriber;
import com.global.college.api.RequestClient;
import com.global.college.base.BaseFragment;
import com.global.college.data.net.HomeGoodListBean;
import com.global.college.data.net.TagListBean;
import com.global.college.utils.StringUtils;
import com.global.college.views.RoundCornerImageview;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class HomeCourseFragment extends BaseFragment {
    @BindView(R.id.recycler_course)
    RecyclerView reclycerCourse;
    @BindView(R.id.tv_course_sort)
    TextView tvCourseSort;
    @BindView(R.id.tv_course_level)
    TextView tvCourseLevel;
    @BindView(R.id.tv_course_subject)
    TextView tvCourseSubject;
    @BindView(R.id.view_trans)
    View transView;
    @BindView(R.id.ll_filter_body)
    LinearLayout llFilterBody;
    @BindView(R.id.recycler_filter)
    RecyclerView recyclerFilter;
    @BindView(R.id.rl_empty)
    RelativeLayout rlEmpty;
    @BindView(R.id.riv_train_cover)
    RoundCornerImageview rivTrainCover;

    private CourseAdapter courseAdapter;
    private FilterItemAdapter filterItemAdapter;

    private boolean sortOpen = false;

    private boolean levelOpen = false;

    private boolean tagOPen = false;
    private int sortBy;
    private long tagId;
    private int level;
    private TagListBean[] tagArray;
    private List<String> tagStringList;
    private List<String> mData;

    @Override
    public int getContentViewId() {
        return R.layout.fragment_lesson;
    }

    @Override
    public void initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        courseAdapter = new CourseAdapter();
        reclycerCourse.setLayoutManager(new LinearLayoutManager(mActivity));
        courseAdapter.bindToRecyclerView(reclycerCourse);
        recyclerFilter.setLayoutManager(new LinearLayoutManager(mActivity));
        filterItemAdapter = new FilterItemAdapter(new FilterItemAdapter.onSelectListener() {
            @Override
            public void onSeclect(int selectNum) {
                onSelectFilterItem(selectNum);
            }
        });
        filterItemAdapter.bindToRecyclerView(recyclerFilter);
    }

    /**
     * 选择过滤条件
     *
     * @param selectNum
     */
    private void onSelectFilterItem(int selectNum) {
        if (sortOpen) {
            sortBy = selectNum;
        } else if (levelOpen) {
            level = selectNum;
        } else if (tagOPen) {
            if (selectNum == 0) {
                tagId = 0;
            } else {
                tagId = tagArray[selectNum - 1].getId();
            }
        }
        getClassData();
        collapseAllFilter();
        llFilterBody.setVisibility(View.GONE);
        transView.setVisibility(View.GONE);
    }

    private void collapseAllFilter() {
        levelOpen = false;
        sortOpen = false;
        tagOPen = false;
        tvCourseLevel.setTextColor(mActivity.getColor(R.color.text_black));
        tvCourseSort.setTextColor(mActivity.getColor(R.color.text_black));
        tvCourseSubject.setTextColor(mActivity.getColor(R.color.text_black));
        tvCourseSort.setCompoundDrawablesWithIntrinsicBounds(null, null, mActivity.getDrawable(R.mipmap.icon_expand_down), null);
        tvCourseLevel.setCompoundDrawablesWithIntrinsicBounds(null, null, mActivity.getDrawable(R.mipmap.icon_expand_down), null);
        tvCourseSubject.setCompoundDrawablesWithIntrinsicBounds(null, null, mActivity.getDrawable(R.mipmap.icon_expand_down), null);
    }

    @Override
    protected void initData() {
        mData = new ArrayList<>();
        tagStringList = new ArrayList<>();
        tagStringList.add("全部");
        getClassData();
        getTagList();
        String cover = ((MainActivity)mActivity).trainCover;
        if (!StringUtils.isEmpty(cover)){
            Glide.with(mActivity).load(cover).into(rivTrainCover);
        }

    }

    @OnClick({R.id.riv_train_cover,R.id.view_trans, R.id.tv_course_sort, R.id.tv_course_level, R.id.tv_course_subject})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.riv_train_cover:
                ((MainActivity)getActivity()).jumpTrain();
                break;
            case R.id.view_trans:
                collapseAllFilter();
                llFilterBody.setVisibility(View.GONE);
                transView.setVisibility(View.GONE);
                break;
            case R.id.tv_course_sort:
                if (sortOpen) {
                    sortOpen = false;
                    tvCourseSort.setTextColor(mActivity.getColor(R.color.text_black));
                    tvCourseSort.setCompoundDrawablesWithIntrinsicBounds(null, null, mActivity.getDrawable(R.mipmap.icon_expand_down), null);
                    llFilterBody.setVisibility(View.GONE);
                    transView.setVisibility(View.GONE);
                } else {
                    collapseAllFilter();
                    sortOpen = true;
                    tvCourseSort.setTextColor(mActivity.getResources().getColor(R.color.color_red));
                    tvCourseSort.setCompoundDrawablesWithIntrinsicBounds(null, null, mActivity.getDrawable(R.mipmap.icon_expand_up), null);
                    llFilterBody.setVisibility(View.VISIBLE);
                    transView.setVisibility(View.VISIBLE);
                    mData.clear();
                    mData.add("默认");
                    mData.add("最新");
                    mData.add("最热");
                    filterItemAdapter.setAdapterType(1);
                    filterItemAdapter.replaceData(mData);
                }
                break;
            case R.id.tv_course_level:
                if (levelOpen) {
                    levelOpen = false;
                    tvCourseLevel.setTextColor(mActivity.getResources().getColor(R.color.text_black));
                    tvCourseLevel.setCompoundDrawablesWithIntrinsicBounds(null, null, mActivity.getDrawable(R.mipmap.icon_expand_down), null);
                    llFilterBody.setVisibility(View.GONE);
                    transView.setVisibility(View.GONE);
                } else {
                    collapseAllFilter();
                    levelOpen = true;
                    tvCourseLevel.setTextColor(mActivity.getResources().getColor(R.color.color_red));
                    tvCourseLevel.setCompoundDrawablesWithIntrinsicBounds(null, null, mActivity.getDrawable(R.mipmap.icon_expand_up), null);
                    llFilterBody.setVisibility(View.VISIBLE);
                    transView.setVisibility(View.VISIBLE);
                    filterItemAdapter.setAdapterType(2);
                    List<String> labelList = new ArrayList<>();
                    labelList.add("全部");
                    labelList.add("初级");
                    labelList.add("中级");
                    labelList.add("高级");
                    filterItemAdapter.replaceData(labelList);
                }
                break;
            case R.id.tv_course_subject:
                if (tagOPen) {
                    tagOPen = false;
                    tvCourseSubject.setTextColor(mActivity.getResources().getColor(R.color.text_black));
                    tvCourseSubject.setCompoundDrawablesWithIntrinsicBounds(null, null, mActivity.getDrawable(R.mipmap.icon_expand_down), null);
                    llFilterBody.setVisibility(View.GONE);
                    transView.setVisibility(View.GONE);
                } else {
                    collapseAllFilter();
                    tagOPen = true;
                    tvCourseSubject.setTextColor(mActivity.getResources().getColor(R.color.red_color_l));
                    tvCourseSubject.setCompoundDrawablesWithIntrinsicBounds(null, null, mActivity.getDrawable(R.mipmap.icon_expand_up), null);
                    llFilterBody.setVisibility(View.VISIBLE);
                    transView.setVisibility(View.VISIBLE);
                    filterItemAdapter.setAdapterType(3);
                    filterItemAdapter.replaceData(tagStringList);
                }
                break;
            default:
                break;
        }
    }

    private void getClassData() {
        RequestClient.getInstance().getGoodListBean(1, sortBy, tagId, level).subscribe(new ProgressSubscriber<HomeGoodListBean>(mActivity, false) {
            @Override
            public void onNext(HomeGoodListBean model) {
                if (model.getGoodList() == null || model.getGoodList().size() == 0) {
                    rlEmpty.setVisibility(View.VISIBLE);
                    reclycerCourse.setVisibility(View.GONE);
                } else {
                    rlEmpty.setVisibility(View.GONE);
                    reclycerCourse.setVisibility(View.VISIBLE);
                    courseAdapter.replaceData(model.getGoodList());
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }

    private void getTagList() {
        RequestClient.getInstance().getTagList().subscribe(new ProgressSubscriber<TagListBean[]>(mActivity, false) {
            @Override
            public void onNext(TagListBean[] model) {
                tagArray = model;
                for (TagListBean bean : model) {
                    tagStringList.add(bean.getNodeName());
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }
}
